package com.pay.web.proxy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import java.io.IOException;

/**
 * Created by Administrator on 2017-7-6.
 */
public class ServletProxy extends GenericServlet {
    private static final Logger logger = LoggerFactory.getLogger(ServletProxy.class);
    private static final long serialVersionUID = 1L;
    private HttpServlet proxy;
    private String targetServletBean;

    @Override
    public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
        logger.debug("==============="+proxy);
        proxy.service(req, resp);
    }

    public void init() throws ServletException {
        this.targetServletBean = this.getInitParameter("targetServletBean");
        this.getServletBean();
        this.proxy.init(this.getServletConfig());
        logger.debug(targetServletBean + " was inited by proxy success");
    }

    private void getServletBean() {
        WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
        this.proxy = (HttpServlet) wac.getBean(targetServletBean);
    }
}
